package com.easychat.controller;

import com.easychat.common.PageResult;
import com.easychat.common.Result;
import com.easychat.dto.TokenUserInfoDto;
import com.easychat.entity.UserInfo;
import com.easychat.entity.constants.Constants;
import com.easychat.service.UserInfoService;
import com.easychat.utils.LoginUserHolder;
import com.easychat.vo.UserInfoVO;
import com.easychat.websocket.ChannelContextUtils;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.util.List;

/**
 * 用户消息表(UserInfo)表控制层
 *
 * @author makejava
 * @since 2024-10-01 17:31:28
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private ChannelContextUtils channelContextUtils;

    /**
     * 分页查询
     *
     * @param userInfo    筛选条件
     * @param page 分页对象
     * @return 查询结果
     */
    @GetMapping("/loadDataList/{page}/{size}")
    public Result<PageResult> queryByPage(@RequestBody(required = false) UserInfo userInfo, @PathVariable int page, @PathVariable int size) {
        System.out.println("***********ok*******");
        PageRequest pageRequest = PageRequest.of(page, size);
        PageResult pageResult = userInfoService.queryByPage(userInfo, pageRequest);
        return Result.ok(pageResult);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public Result<UserInfo> queryById(@RequestParam("userId") String userId) {
        UserInfo userInfo = userInfoService.queryById(userId);
        return Result.ok(userInfo);
    }

    /**
     * 根据userId模糊查询
     */
    @GetMapping("/queryByFuzzyId")
    public Result<PageResult> queryByFuzzyId(@RequestParam("userId") String userId){
        PageRequest pageRequest = PageRequest.of(1, 6);
        PageResult page = userInfoService.queryByFuzzyUserIdPage(userId,pageRequest);
        return Result.ok(page);
    }

    /**
     * 获取用户自己的信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfoVO> getUserInfo(){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        UserInfo userInfo = userInfoService.getUserInfoByUserId(tokenUserInfoDto.getUserId());
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userInfoVO);
        userInfoVO.setAdmin(tokenUserInfoDto.getAdmin());
        return Result.ok(userInfoVO);
    }

    /**
     * 修改用户信息
     * @return
     */
    @GetMapping("/saveUserInfo")
    public Result saveUserInfo(HttpServletRequest request,UserInfo userInfo, MultipartFile avatarFile, MultipartFile avatarCover) throws IOException {
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        userInfo.setUserId(tokenUserInfoDto.getUserId());
        userInfo.setPassword(null);
        userInfo.setStatus(null);
        userInfo.setCreateTime(null);
        userInfo.setLastLoginTime(null);
        userInfoService.updateUserInfo(userInfo,avatarFile,avatarCover);
        return Result.ok(request);
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@NotEmpty @Pattern(regexp = Constants.REGEX_PASSWORD) String password) {
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(password);
        userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfoDto.getUserId());
        //强制退出
        channelContextUtils.closeContext(tokenUserInfoDto.getUserId());
        return Result.ok();
    }
    @PostMapping("/logout")
    public Result logout(@NotEmpty @Pattern(regexp = Constants.REGEX_PASSWORD) String password) {
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
       //强制退出 关闭ws链接
        channelContextUtils.closeContext(tokenUserInfoDto.getUserId());
        return Result.ok();
    }





    /**
     * 新增数据
     *
     * @param userInfo 实体
     * @return 新增结果
     */
    @PostMapping
    public Result add(UserInfo userInfo) {
        boolean insert = userInfoService.insert(userInfo);
        if (insert){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 编辑数据
     *
     * @param userInfo 实体
     * @return 编辑结果
     */
    @PutMapping
    public Result edit(UserInfo userInfo) {
        boolean update = userInfoService.update(userInfo);
        if(update){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public Result deleteById(String id) {
        boolean b = userInfoService.deleteById(id);
        if(b){
            return Result.ok();
        }
        return Result.fail();
    }


}

